package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Accdentdetail;
import cn.wolfcode.crm.domain.Claim;
import cn.wolfcode.crm.domain.Guard;
import cn.wolfcode.crm.domain.Report;
import cn.wolfcode.crm.query.AccentdatailQueryObject;
import cn.wolfcode.crm.service.IAccdentdetailService;
import cn.wolfcode.crm.service.IClaimService;
import cn.wolfcode.crm.service.IGuardService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("accdentdetail")
public class AccdentdetailController {

    @Autowired
    private IAccdentdetailService accdentdetailService;
    @Autowired
    private IClaimService claimService;
    @Autowired
    private IGuardService guardService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"accdentdetail:view","事故明细页面"},logical = Logical.OR)
    public String view() throws Exception{

        return "accdentdetail/accdentdetail";
    }
    @RequestMapping("auditview")
    @RequiresPermissions(value = {"accdentdetail:view","事故明细页面"},logical = Logical.OR)
    public String auditview() throws Exception{

        return "audit/audit";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Accdentdetail> selectAll() throws Exception{
        return accdentdetailService.selectAll();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"accdentdetail:saveOrUpdate","事故明细新增/修改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Accdentdetail entity) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            accdentdetailService.saveOrUpdate(entity );
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }
    @RequestMapping("insertCase")
    @ResponseBody
    @RequiresPermissions(value = {"accdentdetail:insertCase","事故明细案件新增"},logical = Logical.OR)
    public JsonResult insertCase(Report entity) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            accdentdetailService.insertCase(entity);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }


    @RequestMapping("list")
    @ResponseBody
    public PageResult list(AccentdatailQueryObject qo) throws Exception{
        return accdentdetailService.query(qo);
    }
    @RequestMapping("auditquery")
    @ResponseBody
    public PageResult auditlist(AccentdatailQueryObject qo) throws Exception{
        return accdentdetailService.auditquery(qo);
    }
    //上报审核,修改明细状态及审核状态
    @RequestMapping("changeAuditState")
    @ResponseBody
    @RequiresPermissions(value = {"accdentdetail:status","报案表状态修改"},logical = Logical.OR)
    public JsonResult status(Long id,String guardNum) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            Long guardId = guardService.selectIdByGuardNum(guardNum);
            accdentdetailService.insertGuardId(id,guardId);
            accdentdetailService.updateDate(id);
            accdentdetailService.changeAuditState(id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }
    //修改审核状态,添加上报时间
    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions(value = {"accdentdetail:status","报案表状态修改"},logical = Logical.OR)
    public JsonResult changeState(Long ids, Guard guard, Claim claim) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            claimService.changeClaimState(ids);
           accdentdetailService.changeState(ids,guard,claim);
        }catch (Exception e){
            jsonResult.mark(e.getMessage());
        }
        return jsonResult;
    }

}
